<?php

namespace mbilling\BillingBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\RedirectResponse;

use mbilling\BillingBundle\Model\DomainsZonesModel;
use mbilling\BillingBundle\Model\DomainsZonesGatewaysModel;
use mbilling\BillingBundle\Model\UsersAdminLogsModel;
use mbilling\BillingBundle\Entity\domainsZones;
use mbilling\BillingBundle\Entity\usersAdminLogs;

use mbilling\BillingBundle\Form\DomainsZonesForm;

class DomainsZonesController extends Controller
{
    public function indexAction()
    {
        $em = $this->get('doctrine.orm.entity_manager');
        $zone_model = new DomainsZonesModel($em);
        $zones = $zone_model->findAll();
        return $this->render('BillingBundle:Domains_zones:index.html.twig', array('zones' => $zones));
    }
    public function addAction()
    {
        $em = $this->get('doctrine.orm.entity_manager');
        $zone_model = new DomainsZonesModel($em);
        
        $zone = new domainsZones();
        //$zone_model->create($zone->initDefaultEntity());//создание по умолчанию
        //return new RedirectResponse($this->generateUrl('domains_zones_show'));
        
        $gateway_model = new DomainsZonesGatewaysModel($em);
        $gateways = $gateway_model->findAll();
        $gateway_string = array();
        foreach($gateways as $gateway)
        {
            $gateway_string[$gateway->getId()] = $gateway->getName();
        }

        if (count($gateway_string) == 0)
        {
            $errors = array();
            array_push($errors, array('message' => 'Гейтвеи не созданы!', 'link' => 'gateways_add'));
            return $this->render('BillingBundle:Domains_zones:error.html.twig', array('errors' => $errors));
        }
        $form = $this->createForm(new DomainsZonesForm($gateway_string), $zone);

        $request = $this->get('request');
        if ($request->getMethod() == 'POST')
        {
            $form->bindRequest($request);
            if ($form->isValid())
            {
                $zone_model->create($form->getData());
                $securityContext = $this->container->get('security.context');
                $userCurrent = $securityContext->getToken()->getUser();

                $log_model = new UsersAdminLogsModel($em);
                $log = new usersAdminLogs();
                $log->setAdminId($userCurrent->getId());
                $log->setPageId('1');//страница доменные зоны
                $log->setObjectId($zone->getId());//изменяемый объект
                $log->setActionTypeId('1');//создание зоны
                $log_model->create($log);

                return new RedirectResponse($this->generateUrl('domains_zones_show'));
            }
        }
        return $this->render('BillingBundle:Domains_zones:create.html.twig', array('form' => $form->createView()));
    }
    public function updateAction($id)
    {
        $em = $this->get('doctrine.orm.entity_manager');
        $zone_model = new DomainsZonesModel($em);
        $zone = $zone_model->findByPK($id);

        $gateway_model = new DomainsZonesGatewaysModel($em);
        $gateways = $gateway_model->findAll();
        $gateway_string = array();
        foreach($gateways as $gateway)
        {
            $gateway_string[$gateway->getId()] = $gateway->getUrl();
        }
        $form = $this->createForm(new DomainsZonesForm($gateway_string), $zone);

        $request = $this->get('request');
        if ($request->getMethod() == 'POST')
        {
            $form->bindRequest($request);
            if ($form->isValid())
            {
                $zone_model->create($form->getData());
                $securityContext = $this->container->get('security.context');
                $userCurrent = $securityContext->getToken()->getUser();

                $log_model = new UsersAdminLogsModel($em);
                $log = new usersAdminLogs();
                $log->setAdminId($userCurrent->getId());
                $log->setPageId('1');//страница доменные зоны
                $log->setObjectId($zone->getId());//изменяемый объект
                $log->setActionTypeId('2');//обновление зоны
                $log_model->create($log);

                return new RedirectResponse($this->generateUrl('domains_zones_show'));
            }
        }
        return $this->render('BillingBundle:Domains_zones:create.html.twig', array('form' => $form->createView()));
    }
    public function removeAction($id)
    {
        $em = $this->get('doctrine.orm.entity_manager');
        $zone_model = new DomainsZonesModel($em);

        $zone = $zone_model->findByPK($id);
        $securityContext = $this->container->get('security.context');
        
        $zone_model->removeByPK($id);
        $userCurrent = $securityContext->getToken()->getUser();
        $log_model = new UsersAdminLogsModel($em);
        $log = new usersAdminLogs();
        $log->setAdminId($userCurrent->getId());
        $log->setPageId('1');//страница доменные зоны
        $log->setObjectId($id);//изменяемый объект
        $log->setActionTypeId('3');//удаление зоны
        $log_model->create($log);
            
        return new RedirectResponse($this->generateUrl('domains_zones_show'));
    }
}